package com.yunshipei.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;

public class DrawableUtils {
    /**
     * @param strokeWidth 边框宽度
     * @param roundRadius 圆角半径
     * @param strokeColor 边框颜色
     * @param fillColor   内部填充颜色
     * @return
     */
    public static GradientDrawable newRadioDrawable(int strokeWidth, int roundRadius, int strokeColor, int fillColor) {
        GradientDrawable normalState = new GradientDrawable();//创建drawable
        normalState.setColor(fillColor);
        normalState.setCornerRadius(roundRadius);
        normalState.setStroke(strokeWidth, strokeColor);

        return normalState;
    }

    public static StateListDrawable newSelector(Drawable normal, Drawable pressed, Drawable focused, Drawable unable) {
        StateListDrawable bg = new StateListDrawable();

        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focused);
        bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
        bg.addState(new int[]{android.R.attr.state_focused}, focused);
        bg.addState(new int[]{android.R.attr.state_pressed}, pressed);
        bg.addState(new int[]{android.R.attr.state_enabled}, normal);
        bg.addState(new int[]{}, normal);
        return bg;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }

    public static Drawable bitmapToDrawable(Bitmap bitmap, Context mcontext) {
        Drawable drawable = new BitmapDrawable(mcontext.getResources(), bitmap);
        return drawable;
    }

    public static Drawable getCheckBoxDrawable(int color, Drawable topDraw, int width, int height) {

        GradientDrawable bottomDraw = new GradientDrawable();//创建drawable
        bottomDraw.setColor(color);
        bottomDraw.setShape(GradientDrawable.OVAL);
        bottomDraw.setUseLevel(false);
        bottomDraw.setCornerRadius(width / 2);
        bottomDraw.setSize(width, height);

        return getLayer(bottomDraw, topDraw, -1, -1, 0, -1);
    }

    public static StateListDrawable newCheckedSelector(Drawable normal, Drawable pressed) {
        StateListDrawable bg = new StateListDrawable();

        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_checked}, pressed);
        bg.addState(new int[]{android.R.attr.state_checked}, pressed);
        bg.addState(new int[]{}, normal);
        return bg;
    }

    public static LayerDrawable getLayer(Drawable bottomDraw, Drawable topDraw, int left, int top, int right, int bottom) {
        Drawable[] array = new Drawable[2];
        array[0] = bottomDraw;
        array[1] = topDraw;
        LayerDrawable la = new LayerDrawable(array);
        // 其中第一个参数为层的索引号，后面的四个参数分别为left、top、right和bottom
        la.setLayerInset(0, 0, 0, 0, 0);
        la.setLayerInset(1, left, top, right, bottom);

        return la;
    }

    public static Drawable newEditTextBg(int radio, int bottomColor, int topColor, int lineSize) {

        GradientDrawable bottomDraw = newRadioDrawable(0, radio, topColor, topColor);

        bottomDraw.setStroke(lineSize, bottomColor);

        return bottomDraw;
    }

    public static Drawable newProgressBarDraw(Context context, int color) {
        GradientDrawable bottomDraw = new GradientDrawable();
        bottomDraw.setColor(Color.TRANSPARENT);

        GradientDrawable topDraw = new GradientDrawable();
        topDraw.setColor(color);

        LayerDrawable layerDrawable = getLayer(bottomDraw, topDraw, 0, 0, 0, 0);

        layerDrawable.setId(0, android.R.id.background);
        layerDrawable.setId(1, android.R.id.progress);

        return layerDrawable;
    }

    public static Drawable getYspCheckBoxDraw(int color, Drawable normalDraw, Drawable inIcon) {
        Drawable top = DrawableUtils.getCheckBoxDrawable(color, inIcon, normalDraw.getIntrinsicWidth(), normalDraw.getIntrinsicHeight());
        Drawable cDraw = DrawableUtils.newCheckedSelector(normalDraw, top);
        return cDraw;
    }
}
